//
//  ListViewController.m
//  Mahjong
//
//  Created by KING on 16/9/11.
//  Copyright © 2016年 swift. All rights reserved.
//

#import "ListViewController.h"
#import "MoreSelectView.h"
#import "Header.h"
#import "MineAdressTableViewCell.h"
#import "MineAdressTitleTableViewCell.h"
#import "MineAdressPreTableViewCell.h"
#import "MineAdressButtonTableViewCell.h"
#import "MineAdressDetailViewController.h"

#import "MineListAdressTableViewCell.h"
#import "MineListConTableViewCell.h"

#import "ListNumberTableViewCell.h"
#import "ListContentTableViewCell.h"
#import "ListDiyViewController.h"
#import "ListDetailViewController.h"
@interface ListViewController ()
<selectDelegate,UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) MoreSelectView *selectView;
@property (nonatomic, strong) NSArray *topArray;
@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic, assign) NSInteger number;
@property (nonatomic, strong) UITableView *dingTabelView;
@property (nonatomic, assign) NSInteger did;
@end

@implementation ListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    _selectView = [[MoreSelectView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 40)];
    [_selectView.oneButton setTitle:@"购物订单" forState:UIControlStateNormal];
    [_selectView.twoButton setTitle:@"定制订单" forState:UIControlStateNormal];
    [self.view addSubview:_selectView];
    _selectView.myDelegate = self;
    //选择按钮创建
    [self addTopView];
    [self addTopTwoView];
    UIButton *b = (UIButton *)[self.view viewWithTag:200];
    UIButton *c = (UIButton *)[self.view viewWithTag:201];
    UIButton *d = (UIButton *)[self.view viewWithTag:202];
    UIButton *e = (UIButton *)[self.view viewWithTag:203];
    b.alpha = 0;c.alpha = 0;d.alpha = 0;e.alpha = 0;
    //购物订单
    [self createmyTableView];
    //定制订单
    [self createdingTableView];
    self.did = 0;
    
}
-(void)createmyTableView{
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 81, SCREEN_WIDTH, SCREENH_HEIGHT - 90) style:UITableViewStylePlain];
    self.myTableView.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:239/255.0 alpha:1];
    [self.view addSubview:self.myTableView];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.myTableView registerNib:[UINib nibWithNibName:@"ListNumberTableViewCell" bundle:nil] forCellReuseIdentifier:@"numberCell"];
    [self.myTableView registerNib:[UINib nibWithNibName:@"ListContentTableViewCell" bundle:nil] forCellReuseIdentifier:@"conCell"];
}
-(void)createdingTableView{
    self.dingTabelView = [[UITableView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH, 81, SCREEN_WIDTH, SCREENH_HEIGHT - 90) style:UITableViewStylePlain];
    self.dingTabelView.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:239/255.0 alpha:1];
    [self.view addSubview:self.dingTabelView];
    self.dingTabelView.delegate = self;
    self.dingTabelView.dataSource = self;
    self.dingTabelView.separatorStyle = UITableViewCellSeparatorStyleNone;

    [self.dingTabelView registerNib:[UINib nibWithNibName:@"ListNumberTableViewCell" bundle:nil] forCellReuseIdentifier:@"dnumberCell"];
    [self.dingTabelView registerNib:[UINib nibWithNibName:@"MineListAdressTableViewCell" bundle:nil] forCellReuseIdentifier:@"dcell"];
    [self.dingTabelView registerNib:[UINib  nibWithNibName:@"MineListConTableViewCell" bundle:nil] forCellReuseIdentifier:@"dconcell"];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.myTableView) {
        
    if (indexPath.row == 0) {
        ListNumberTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"numberCell"];
        
        return cell;
    }
    if (indexPath.row == 1) {
        ListContentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"conCell"];
        return cell;
    }else{
        NSMutableAttributedString *abs = [[NSMutableAttributedString alloc] initWithString:@"共2件商品 合计:¥1234(运费20)"];
        [abs beginEditing];
        [abs addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:22] range:NSMakeRange(1, 1)];
        [abs addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(1, 1)];
        [abs addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:22] range:NSMakeRange(9, 5)];
        [abs addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(9, 5)];
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        cell.textLabel.attributedText = abs;
        
        
    return cell;
    }
    }
    else {
    
        if (indexPath.row == 0) {
            ListNumberTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"dnumberCell"];
            return cell;
        }if (indexPath.row == 1) {
            MineListAdressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"dcell"];
            
            return cell;

        } else {
            MineListConTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"dconcell"];
            
            return cell;

        }
    }

}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 1) {
        return 60;
    }
    return 40;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.did == 0) {
        ListDetailViewController *detail = [[ListDetailViewController alloc] init];
        detail.didNum = self.number;
        detail.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:detail animated:YES];
    }
    if (self.did == 1) {
        ListDiyViewController *detail = [[ListDiyViewController alloc] init];
        detail.diyNum = self.number;
        detail.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:detail animated:YES];
    }
}

-(void)oneSelectAction{
    self.did = 0;
    self.myTableView.frame = CGRectMake(0, 81, SCREEN_WIDTH, SCREENH_HEIGHT - 90);
    self.dingTabelView.frame = CGRectMake(SCREEN_WIDTH, 81, SCREEN_WIDTH, SCREENH_HEIGHT - 90);
    UIButton *ab = (UIButton *)[self.view viewWithTag:100];
    UIButton *ac = (UIButton *)[self.view viewWithTag:101];
    UIButton *ad = (UIButton *)[self.view viewWithTag:102];
    UIButton *ae = (UIButton *)[self.view viewWithTag:103];
    
    ab.alpha = 1;ac.alpha = 1;ad.alpha = 1;ae.alpha = 1;
    
    
    UIButton *b = (UIButton *)[self.view viewWithTag:200];
    UIButton *c = (UIButton *)[self.view viewWithTag:201];
    UIButton *d = (UIButton *)[self.view viewWithTag:202];
    UIButton *e = (UIButton *)[self.view viewWithTag:203];
    
    b.alpha = 0;c.alpha = 0;d.alpha = 0;e.alpha = 0;
}
-(void)twoSelectAction{
    self.did = 1;
    self.dingTabelView.frame = CGRectMake(0, 81, SCREEN_WIDTH, SCREENH_HEIGHT - 90);
    self.myTableView.frame = CGRectMake(SCREEN_WIDTH, 81, SCREEN_WIDTH, SCREENH_HEIGHT - 90);
    UIButton *ab = (UIButton *)[self.view viewWithTag:100];
    UIButton *ac = (UIButton *)[self.view viewWithTag:101];
    UIButton *ad = (UIButton *)[self.view viewWithTag:102];
    UIButton *ae = (UIButton *)[self.view viewWithTag:103];
    
    ab.alpha = 0;ac.alpha = 0;ad.alpha = 0;ae.alpha = 0;
    
    
    UIButton *b = (UIButton *)[self.view viewWithTag:200];
    UIButton *c = (UIButton *)[self.view viewWithTag:201];
    UIButton *d = (UIButton *)[self.view viewWithTag:202];
    UIButton *e = (UIButton *)[self.view viewWithTag:203];
    
    b.alpha = 1;c.alpha = 1;d.alpha = 1;e.alpha = 1;
}
-(void)addTopView{
    self.titleArray = @[@"待付款",@"待发货",@"待收货",@"待评价"];
    for (int i = 0; i <self.titleArray.count; i++) {
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(i*SCREEN_WIDTH/self.titleArray.count, 40, SCREEN_WIDTH/self.titleArray.count, 40);
        [button setTitle:self.titleArray[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self.view addSubview:button];
        button.tag = 100+i;
        [button addTarget:self action:@selector(topAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    UIButton *a = (UIButton *)[self.view viewWithTag:100];
    [a setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    self.number = 100;
}
-(void)addTopTwoView{
    NSArray *title = @[@"待确认",@"已报价",@"未完成",@"已交付"];
    for (int i = 0; i <title.count; i++) {
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(i*SCREEN_WIDTH/title.count, 40, SCREEN_WIDTH/title.count, 40);
        [button setTitle:title[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self.view addSubview:button];
        button.tag = 200+i;
        [button addTarget:self action:@selector(topAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    UIButton *a = (UIButton *)[self.view viewWithTag:200];
    [a setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    self.number = 200;
   
}
-(void)topAction:(UIButton *)btn{
    
    if (btn.tag == 100) {
        UIButton *a = (UIButton *)[self.view viewWithTag:100];
        [a setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        UIButton *b = (UIButton *)[self.view viewWithTag:101];
        [b setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        UIButton *c = (UIButton *)[self.view viewWithTag:102];
        [c setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        UIButton *d = (UIButton *)[self.view viewWithTag:103];
        [d setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        UIButton *e = (UIButton *)[self.view viewWithTag:104];
        [e setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        self.number = 100;
        
    }
    if (btn.tag == 101) {
        UIButton *a = (UIButton *)[self.view viewWithTag:100];
        [a setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        UIButton *b = (UIButton *)[self.view viewWithTag:101];
        [b setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        UIButton *c = (UIButton *)[self.view viewWithTag:102];
        [c setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        UIButton *d = (UIButton *)[self.view viewWithTag:103];
        [d setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        UIButton *e = (UIButton *)[self.view viewWithTag:104];
        [e setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        self.number = 101;
        
    }
    if (btn.tag == 102) {
        UIButton *a = (UIButton *)[self.view viewWithTag:100];
        [a setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        UIButton *b = (UIButton *)[self.view viewWithTag:101];
        [b setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        UIButton *c = (UIButton *)[self.view viewWithTag:102];
        [c setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        UIButton *d = (UIButton *)[self.view viewWithTag:103];
        [d setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        UIButton *e = (UIButton *)[self.view viewWithTag:104];
        [e setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        self.number = 102;
        
    }if (btn.tag == 103) {
        UIButton *a = (UIButton *)[self.view viewWithTag:100];
        [a setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        UIButton *b = (UIButton *)[self.view viewWithTag:101];
        [b setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        UIButton *c = (UIButton *)[self.view viewWithTag:102];
        [c setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        UIButton *d = (UIButton *)[self.view viewWithTag:103];
        [d setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        UIButton *e = (UIButton *)[self.view viewWithTag:104];
        [e setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        self.number = 103;
        
    }
        if (btn.tag == 200) {
        UIButton *a = (UIButton *)[self.view viewWithTag:200];
        [a setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        UIButton *b = (UIButton *)[self.view viewWithTag:201];
        [b setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        UIButton *c = (UIButton *)[self.view viewWithTag:202];
        [c setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        UIButton *d = (UIButton *)[self.view viewWithTag:203];
        [d setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        self.number = 200;
        
    }
    if (btn.tag == 201) {
        UIButton *a = (UIButton *)[self.view viewWithTag:200];
        [a setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        UIButton *b = (UIButton *)[self.view viewWithTag:201];
        [b setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        UIButton *c = (UIButton *)[self.view viewWithTag:202];
        [c setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        UIButton *d = (UIButton *)[self.view viewWithTag:203];
        [d setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        self.number = 201;
        
    }
    if (btn.tag == 202) {
        UIButton *a = (UIButton *)[self.view viewWithTag:200];
        [a setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        UIButton *b = (UIButton *)[self.view viewWithTag:201];
        [b setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        UIButton *c = (UIButton *)[self.view viewWithTag:202];
        [c setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        UIButton *d = (UIButton *)[self.view viewWithTag:203];
        [d setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
      
        self.number =202;
        
    }if (btn.tag == 203) {
        UIButton *a = (UIButton *)[self.view viewWithTag:200];
        [a setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        UIButton *b = (UIButton *)[self.view viewWithTag:201];
        [b setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        UIButton *c = (UIButton *)[self.view viewWithTag:202];
        [c setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        UIButton *d = (UIButton *)[self.view viewWithTag:203];
        [d setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
       
        self.number = 203;
        
    }
        [self.myTableView reloadData];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
